package com.sino.operation.controller;

import com.alibaba.fastjson.JSONObject;
import com.sino.operation.commons.ConstantValue;
import com.sino.operation.commons.response.CenterResponseEntity;
import com.sino.operation.dto.Dto;
import com.sino.operation.dto.out.TaskQueueDto;
import com.sino.operation.entity.SysCenter;
import com.sino.operation.param.out.EscBodyByCounterInquire;
import com.sino.operation.param.out.EscReceiveInquireByCounterParam;
import com.sino.operation.peripheral.esc.request.EscBody;
import com.sino.operation.peripheral.esc.request.EscRequestParam;
import com.sino.operation.peripheral.esc.request.NetHandle;
import com.sino.operation.peripheral.esc.response.EscResponseDto;
import com.sino.operation.peripheral.esc.response.EscRet;
import com.sino.operation.service.BizAddImageService;
import com.sino.operation.service.SysCenterService;
import com.sino.operation.service.business.ComplateTaskService;
import com.sino.operation.service.business.NetHandleListService;
import com.sino.operation.service.business.TerminalTaskService;
import com.sino.operation.util.EscResTest;
import com.sino.operation.vo.PublicInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName OutSystemController
 * @Description TOD
 * @Author 北陈
 * @Date 2021/10/9 11:33
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("outSystem")
@Api(value = "外围系统查询等操作",tags = "外围系统查询、终止等操作")
public class OutSystemController {

    @Autowired
    private SysCenterService sysCenterService;
    @Autowired
    private NetHandleListService netHandleListService;
    @Autowired
    private TerminalTaskService terminalTaskService;
    @Autowired
    private BizAddImageService bizAddImageService;
    @Autowired
    private ComplateTaskService complateTaskService;

    @PostMapping("complateAddImage")
    @ApiOperation(value = "补扫完成")
    public EscResponseDto complateAddImage(@RequestBody @Valid EscReceiveInquireByCounterParam escRequestParam){
        System.out.println(escRequestParam.toString());
        EscResponseDto escReponse = EscResTest.getEscReponse();
        escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.build());
    return   escReponse;

    }
    @PostMapping("repeatHandleResult")
    @ApiOperation(value = "判重结果处理")
    public EscResponseDto repeatHandleResult(@RequestBody @Valid EscReceiveInquireByCounterParam escRequestParam) {
        System.out.println(escRequestParam.toString());
        EscResponseDto escReponse = EscResTest.getEscReponse();
        escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.build());
        return escReponse;
      }

    @PostMapping("CenterCheck")
    @ApiOperation(value = "中心异常检查")
    public EscResponseDto CenterCheck(@RequestBody @Valid EscReceiveInquireByCounterParam escReceiveInquireByCounterParam) {
        EscBodyByCounterInquire escBody = escReceiveInquireByCounterParam.getEscBodyByCounterInquire();
        String orgNo = escBody.getInquirePublicParam().getOrgCode();
        log.debug("柜面签退柜面柜员"+orgNo);
        SysCenter sysCenter = sysCenterService.selectCenterByOrgNo(orgNo);
        EscResponseDto escReponse = EscResTest.getEscReponse();
        if (sysCenter==null){
            escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.build().setMsg("中心不存在").setCode(ConstantValue.NOT_RESULT));
            return escReponse;
        }
        escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.build(SysCenter.class).setData(sysCenter));
        return escReponse;
    }

    @PostMapping("TerminalTask")
    @ApiOperation(value = "终止任务")
    public EscResponseDto TerminalTask(@RequestBody @Valid EscReceiveInquireByCounterParam escReceiveInquireByCounterParam) {
        EscBodyByCounterInquire escBody = escReceiveInquireByCounterParam.getEscBodyByCounterInquire();
        boolean result = terminalTaskService.terminaHandle(escBody);
        EscResponseDto escReponse = EscResTest.getEscReponse();
        if (result){
            escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.build().setMsg(ConstantValue.TERMINAL_SUCESS));
        }
        escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.buildError().setMsg(ConstantValue.TERMINAL_ERROR));
        return escReponse;
    }
    @PostMapping("ComplateTask")
    @ApiOperation(value = "提交任务")
    public EscResponseDto ComplateTask(@RequestBody @Valid EscReceiveInquireByCounterParam escReceiveInquireByCounterParam) {
        EscBodyByCounterInquire escBody = escReceiveInquireByCounterParam.getEscBodyByCounterInquire();
        boolean result = complateTaskService.taskHandle(escBody);
        EscResponseDto escReponse = EscResTest.getEscReponse();
        if (result){
            escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.build().setMsg(ConstantValue.COMPLATE_SUCESS));
        }
        escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.buildError().setMsg(ConstantValue.COMPLATE_ERROR));
        return escReponse;
    }
    @PostMapping("CounterSign")
    @ApiOperation(value = "柜面签退柜员查询任务接口")
    public EscResponseDto CounterSignByCenter(@RequestBody @Valid EscReceiveInquireByCounterParam escReceiveInquireByCounterParam){
        EscBodyByCounterInquire escBody = escReceiveInquireByCounterParam.getEscBodyByCounterInquire();
        Map map = (Map)escBody.getObject();
        String tellNo = (String) map.get("TELLER_NO");
        log.debug("柜面签退柜面柜员"+tellNo);
        Integer integer = bizAddImageService.counterSignByCenter(tellNo);
        EscResponseDto escReponse = EscResTest.getEscReponse();
        CenterResponseEntity<Map> entity = CenterResponseEntity.build(Map.class).setMsg("查询成功");
        Map<String, Integer> task = new HashMap<>(16);
        task.put("TASK_NUM",integer);
        entity.setData(task);
        escReponse.getEscResBody().setCenterResponseEntity(entity);
        return escReponse;
    }

    @PostMapping("NetHandleList")
    @ApiOperation(value = "网点查询已处理任务队列")
    public EscResponseDto selectNetHandleList(@RequestBody @Valid EscReceiveInquireByCounterParam escRequestParam) {
        Object object= escRequestParam.getEscBodyByCounterInquire().getObject();
        String json = JSONObject.toJSONString(object);
        NetHandle netHandle = JSONObject.parseObject(json, NetHandle.class);
        Dto data = netHandleListService.execute(netHandle);
        EscResponseDto escReponse = EscResTest.getEscReponse();
        escReponse.getEscResBody().setCenterResponseEntity(CenterResponseEntity.build(Dto.class).setData(data));
        return escReponse;
    }
}
